// pages/business_list/business_list.js
const app = getApp(), core = require('../../utils/core'), util = require('../../utils/util');
Page({

  /**
   * 页面的初始数据
   */
  data: {

    business: [],

    companyList: [],
    companyListMore: {page:1},
    showFilter:false,//显示条件弹出层
    filterTabIndex:0,//条件层tab index

    filterTab1SubIndex: "0",
    filterTab2SubIndex: "0",
    filterTab3SubIndex: "0",
    query: {},

    categories: [],
    districts: [],
    nearbyArr: [1,3,5,7,10],
    scales:[]
  },

  tab:function(event) {
    if(this.data.showFilter && this.data.filterTabIndex==event.currentTarget.dataset.index) {
      this.hideFilter();
    }else {
      this.setData({showFilter: true, filterTabIndex: event.currentTarget.dataset.index});
      wx.setNavigationBarColor({
        frontColor: '#000000', // 必写项
        backgroundColor: '#ffffff', // 必写项
        animation: { // 可选项
          duration: 400,
          timingFunc: 'easeIn'
        }
      });
    }
  },
  hideFilter() {
    this.setData({showFilter:false});
    wx.setNavigationBarColor({
      frontColor: '#ffffff', // 必写项
      backgroundColor: '#ff7100', // 必写项
      animation: { // 可选项
        duration: 400,
        timingFunc: 'easeIn'
      }
    });
  },
  onLoad: function (options) {
    let page = this;
    var query = wx.createSelectorQuery();
    query.select('.tab').boundingClientRect(function (res) {
      page.setData({
        offset_top:res.top
      })
    }).exec();

    this.setData({currentCity: app.getCurrentCity()});

    util.getDistricts().then(function(districts) {page.setData({districts: districts});});
    util.getIndustryCategories().then(industryCategories=>{page.setData({categories:industryCategories});});
    util.getCompanyScales().then(function(scales){page.setData({scales:scales})});
    //获取定位
    wx.getLocation({
      success: res => {
        page.setData({'query.pos': res.longitude+','+res.latitude});
      },
      fail: res => {
        console.log(res);
      }
    });
    this.fetch();
    app.eventBus.on(app.Events.CurrentCityChange, this.eventOnCurrentCityChange, this);
  },
  onUnload: function () {
    app.eventBus.off(app.Events.CurrentCityChange, this.eventOnCurrentCityChange);
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if(this.data.refreshOnShow) {
      this.setData({refreshOnShow:false});
      this.refresh(true);
    }
  },
  refresh: function (clearQuery=false) {
    this.setData({companyList:[], companyListMore:{page:1},showFilter:false});
    if(clearQuery)
      this.setData({query:{}});
    
    this.fetch();
  },
  fetch: function () {
    util.fetch(this, '/xilujob.company/lists', this.data.query, 'companyListMore', 'companyList', 'data');
  },
  bindResetFormQuery: function () {
    this.setData({
      'query.work_experience_id': '', 'query.type': '', 'query.education_level_id': '', 'query.benefit_id': '', 'query.refresh_days': ''
    });
  },
  bindFormQueryConfirm: function () {
    this.refresh();
  },
  bindTabSubIndexChange: function (event) {
    let param = event.currentTarget.dataset.param, index = event.currentTarget.dataset.index;
    this.setData({[param]: index});
  },

  bindCategoryChange: function (event) {
    let id = event.currentTarget.dataset.id;
    this.setData({
      'query.category_id': id,
    });
    this.refresh();
  },

  bindScaleChange: function (event) {
    let id = event.currentTarget.dataset.id;
    this.setData({
      'query.scale_id': id,
    });
    this.refresh();
  },
  //选择附近
  bindDistanceChange: function(event) {
    if(!this.data.query.pos) {
      wx.showToast({title: '定位失败,请到设置同意授权',icon:'none'});
      return;
    }
    this.setData({
      'query.distance': event.currentTarget.dataset.distance,
    });
    this.refresh();
  },
  //选择区
  bindDistrictChange: function(event) {
    let district = event.currentTarget.dataset.district;
    this.setData({
      'query.district_id': event.currentTarget.dataset.id,
      'filterTab2Name': district?district.name:'不限',
      'query.distance':''
    });
    this.refresh();
  },
  //选择薪资
  bindSalaryOptionChange: function (event) {
    this.setData({
      'query.salary_type': event.currentTarget.dataset.type,
      'query.salary_min': event.currentTarget.dataset.min,
      'query.salary_max': event.currentTarget.dataset.max,
      'query.salary_option_id': event.currentTarget.dataset.id,
    });
    this.refresh();
  },
  //
  bindQueryParamChange: function (event) {
    let param = event.currentTarget.dataset.param, value = event.currentTarget.dataset.value;
    this.setData({['query.'+param]: value});
  },
  eventOnCurrentCityChange: function (currentCity) {
    this.setData({currentCity});
    let pages = getCurrentPages();
    //如果当前页面正在显示, 刷新此页； 否则下次显示时刷新
    if(pages[pages.length-1]===this) {
      this.refresh(true);
    }else {
      this.setData({refreshOnShow: true});
    }
    //悄悄更新区数据
    util.getDistricts().then(districts=>{
      this.setData({districts});
    });
  }


});
